package norbert.containers {
	import flash.display.DisplayObject;
	
	import mx.containers.Canvas;
	import mx.core.ScrollPolicy;
	import mx.core.UIComponent;
	import mx.logging.ILogger;
	import mx.logging.Log;

	public class Desktop extends Canvas {
		/* PROPERTIES */
		
		private static var logger: ILogger = Log.getLogger("norbert.containers.Desktop");
		
		public var floatingLeft: Boolean = false;
		public var floatingTop: Boolean = false;
		public var floatingRight: Boolean = false;
		public var floatingBoottom: Boolean = false;
		
		private var dockLeft: ToolBox;
		private var dockTop: ToolBox;
		private var dockRight: ToolBox;
		private var dockBottom: ToolBox;
		
		/* CONSTRUCTOR */
		
		public function Desktop() {
			minWidth = minHeight = 100;
			percentWidth = percentHeight = 100;
			horizontalScrollPolicy = ScrollPolicy.OFF;
			verticalScrollPolicy = ScrollPolicy.OFF;

			dockLeft = new ToolBox();
			dockTop= new ToolBox();
			dockRight= new ToolBox();
			dockBottom= new ToolBox();
			
			dockLeft.width = dockRight.width = 250;
			dockLeft.percentHeight = dockRight.percentHeight = 100;
			dockLeft.minHeight = dockRight.minHeight = 100;
			
			dockTop.percentWidth = 100;
		}

		/* METHODS */
		
		override public function addChild(child: DisplayObject): DisplayObject {
			if (child != null && child is IToolPanel) {
				if (IToolPanel(child).direction == ToolPanelDirection.LEFT) {
					return dockLeft.addChild(child);
				} else if (IToolPanel(child).direction == ToolPanelDirection.TOP) {
					return dockTop.addChild(child);
				} else if (IToolPanel(child).direction == ToolPanelDirection.RIGHT) {
					return dockRight.addChild(child);
				} else if (IToolPanel(child).direction == ToolPanelDirection.BOTTOM) {
					return dockBottom.addChild(child);
				}
			}
			return super.addChild(child);
		}
		
		override protected function createChildren(): void {
			super.createChildren();
			
			if (dockLeft) {
				addChild(dockLeft);
			}
			if (dockTop) {
				addChild(dockTop);
			}
			if (dockRight) {
				addChild(dockRight);
			}
			if (dockBottom) {
				addChild(dockBottom);
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number): void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			var item: Object;
			
			if (dockTop) {
				for each(item in getChildren()) {
					if (item != dockTop) {
						if (!floatingTop) {
							UIComponent(item).y = dockTop.getExplicitOrMeasuredHeight();
						} else {
							UIComponent(item).y = 0;
						}
					}
				}
			}

			if (dockLeft) {
				for each(item in getChildren()) {
					if (!(item is ToolBox)) {
						if (!floatingLeft) {
							UIComponent(item).x = dockLeft.getExplicitOrMeasuredWidth();
						} else {
							UIComponent(item).x = 0;
						}
					}
				}
			}
			
			if (dockRight) {
				dockRight.x = width - dockRight.getExplicitOrMeasuredWidth();
				
				if (!floatingRight) {
					for each(item in getChildren()) {
						if (!(item is ToolBox)) {
							var child: UIComponent = UIComponent(item);
							child.setActualSize(child.width - dockRight.getExplicitOrMeasuredWidth(), child.height);
						}
					}
				}
			}
		}
	}
}
